#!/usr/bin/env python
import sys
sys.path.append("/work/opt/analytic/my_python/lib")
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from jpype import *
import glob
import sys
import re
import os
#sys.setdefaultencoding('utf-8')

class Service58(object):
    def __init__(self):
        self.jars = ':'.join(glob.glob('./java_lib/imc/*.jar'))
        startJVM(getDefaultJVMPath(),"-ea","-Djava.class.path=%s" % self.jars)
        self.SCFInit =JClass("com.bj58.spat.scf.client.SCFInit")
        self.SCFInit.init('./config/scf.config')
        
        URL= "tcp://imc/InfoProviderWithIndex"            
        self.ProxyFactory =JClass("com.bj58.spat.scf.client.proxy.builder.ProxyFactory")
        #self.IProductService =JClass("com.bj58.sfft.imc.cntity.Info")
        self.Info =JClass("com.bj58.sfft.imc.contract.IInfoService")
        self.Integer =JClass("java.lang.Integer")
        self.proxy = self.ProxyFactory.create(self.Info,URL)
        self.out = JClass("com.bj58.spat.scf.server.contract.entity.Out")
        #self.out_total = self.out{}
        print "ok"
        
    def get_info(self,user_Id):
        self.user_Id = user_Id
        self.query = "userid=%s"%user_Id
        #print self.query
        self.total = self.out()
        self.total.setOutPara(0)
        self.info_count = self.proxy.GetInfoCount(self.query)
        
        return self.info_count
    
    def shut_down(self):
        shutdownJVM()
    
    
#mysql - stats
def set_open_conn():    
    conn = HandleMysql('10.3.10.50','58885','ec_stat','L#c8UM5RGh5C','dbwww58com_58_stats')
    conn.connect()
    return conn

#logic
def execute( param ):
    conn = set_open_conn();
    s58=Service58()
    stat_table = "user_post_info_stats"
    
    query = "select user_id from user_id_tab  order by user_id asc;"
    items = conn.get_results(query)
    for item in items:
        user_id = item[0]
        #print user_id
        try:
            info_count = s58.get_info(user_id)
            param=[user_id,info_count]
            #print param
            conn.insert_record(stat_table,param)
        except Exception,e:
            continue;
def main():

    #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)

if __name__=='__main__':
    main()